<template>
  <a-modal :title="title" :width="width" :visible="visible" @ok="handleCancel"
    :okButtonProps="{ class: { 'jee-hidden': disableSubmit } }" @cancel="handleCancel" cancelText="关闭">
    <BasicTable @register="registerTable" :loading="loading" :pagination="{ pageSize: 20 }">
      <template #action="{ record }">
        <TableAction :actions="getTableAction(record)" />
      </template>
    </BasicTable>
  </a-modal>

  <TableModal @register="registerItemsModal" ></TableModal>
</template>

<script lang="ts" setup>
import { ref, nextTick, defineExpose } from 'vue';
import { useListPage } from '/@/hooks/system/useListPage';
import { itemRecordslist } from '../LabWscientific.api';
import { columnsRecord } from '../LabWscientific.data';
import { BasicTable, TableAction } from '/@/components/Table';
import { useModal } from '/@/components/Modal';
import TableModal from '../../items_check/LabCheckItemsFrom.vue';
import axios from 'axios';
const loading = ref(false);
const title = ref<string>('');
const width = ref<number>(1000);
const visible = ref<boolean>(false);
const disableSubmit = ref<boolean>(false);
const emit = defineEmits(['register', 'success']);
let queryParam = {}
// 注册table数据
const { tableContext } = useListPage({
  tableProps: {
    api: itemRecordslist,
    columns: columnsRecord,
    canResize: false,
    useSearchForm: false,
    showIndexColumn: true,
    showTableSetting: false,
    showActionColumn: true,
    actionColumn: {
      width: 150,
      fixed: 'right',
    },
    beforeFetch: (params) => {
      return Object.assign(params, queryParam);
    },
  },
});

const [registerTable, { reload }] = tableContext;

const [registerItemsModal, { openModal }] = useModal();
/**
 * 编辑
 * @param record
 */
function openModel(record) {
  title.value = '实验记录';
  visible.value = true;
  queryParam = record;
  nextTick(() => {
    reload();
  });
}
/**
 * 取消按钮回调事件
 */
function handleCancel() {
  visible.value = false;
}

function handleItemRecords(record) {
  openModal(true, {
    record,
    disabled: true,
  });
}
const instancea = axios.create({
    baseURL: 'http://10.89.208.24:9999',
    headers: {
      responseType: 'blob',
      ContentType: 'multipart/json, charset=UTF-8',
      timeout: 1000000,
      type: 'multipart/form-data',
    },
  });
/**
   *  下载并返回。
*/
   function downloadt(e) {
    instancea.defaults.responseType = 'blob';
    instancea
      .post('/entrust/labEntrust/down_load_experiment/' + e.id+'/'+e.itemName)
      .then((response) => {
        // 处理返回的数据
        const fileName = e.entrustType+'-'+ e.itemName + '.pdf';
        const elink = document.createElement('a');
        elink.download = fileName;
        elink.style.display = 'none';
        let binaryData = [];
        binaryData.push(response.data);
        elink.href = window.URL.createObjectURL(new Blob(binaryData));
        document.body.appendChild(elink);
        elink.click();
        URL.revokeObjectURL(elink.href); // 释放URL 对象
        document.body.removeChild(elink);
      })
      .catch((error) => {
        // 处理错误
        console.error(error);
      });
  }
function getTableAction(record) {
  if(record.itemStatus == "待实验"){
    return [
      {
        label: '实验记录',
        onClick: handleItemRecords.bind(null, record),
      },
    ];
  }else{
    return [
      {
        label: '实验记录',
        onClick: handleItemRecords.bind(null, record),
      },
      {
        label: '下载',
        onClick: downloadt.bind(null, record),
      },
    ];
  }

  }

defineExpose({
  openModel,
  reload,
});
</script>

<style>
/**隐藏样式-modal确定按钮 */
.jee-hidden {
  display: none !important;
}
</style>
